package com.ruoyi.business.controller;

import com.ruoyi.business.service.ClassPermissionsService;
import com.ruoyi.business.service.IWsdLeaveRecordsService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.PayoutsDTO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WsdLeaveRecords;
import com.ruoyi.common.core.domain.entity.WsdLeaves;
import com.ruoyi.common.core.domain.entity.WsdMealRefunds;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 请假审批记录Controller
 *
 * @author ruoyi
 * @date 2024-04-16
 */
@RestController
@RequestMapping("wsd_records")
public class WsdLeaveRecordsController extends BaseController {

    @Autowired
    private IWsdLeaveRecordsService wsdLeaveRecordsService;
    @Autowired
    private ClassPermissionsService classPermissionsService;
    /**
     * 查询请假审批记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:records:list')")
    @GetMapping("/list")
    public TableDataInfo list(WsdLeaveRecords wsdLeaveRecords) {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        if (!user.isAdmin()) {
            wsdLeaveRecords.setSchoolId(schoolId);
            //班级权限 过滤掉学校管理员和admin
            if (!"business".equals( user.getRoles().get( 0 ).getRoleKey() )) {
                String classIds = classPermissionsService.classIds();
                wsdLeaveRecords.setClassIds( classIds );
            }
        }
        startPage();
        List<WsdLeaveRecords> list = wsdLeaveRecordsService.selectWsdLeaveRecordsList(wsdLeaveRecords);
        return getDataTable(list);
    }

    /**
     * 导出请假审批记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:records:export')")
    @Log(title = "请假审批记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WsdLeaveRecords wsdLeaveRecords)
    {
        List<WsdLeaveRecords> list = wsdLeaveRecordsService.selectWsdLeaveRecordsList(wsdLeaveRecords);
        ExcelUtil<WsdLeaveRecords> util = new ExcelUtil<WsdLeaveRecords>(WsdLeaveRecords.class);
        util.exportExcel(response, list, "请假审批记录数据");
    }

    /**
     * 获取请假审批记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:records:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wsdLeaveRecordsService.selectWsdLeaveRecordsById(id));
    }

    /**
     * 新增请假审批记录
     */
    @PreAuthorize("@ss.hasPermi('business:records:add')")
    @Log(title = "请假审批记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WsdLeaveRecords wsdLeaveRecords) {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        wsdLeaveRecords.setSchoolId(schoolId);
        return toAjax(wsdLeaveRecordsService.insertWsdLeaveRecords(wsdLeaveRecords));
    }

    /**
     * 修改请假审批记录
     */
    @PreAuthorize("@ss.hasPermi('business:records:edit')")
    @Log(title = "请假审批记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WsdLeaveRecords wsdLeaveRecords)
    {
        return toAjax(wsdLeaveRecordsService.updateWsdLeaveRecords(wsdLeaveRecords));
    }

    /**
     * 删除请假审批记录
     */
    @PreAuthorize("@ss.hasPermi('business:records:remove')")
    @Log(title = "请假审批记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wsdLeaveRecordsService.deleteWsdLeaveRecordsByIds(ids));
    }

    /**
     * 审批请假请求
     */
    @PreAuthorize("@ss.hasPermi('business:leaves:approval')")
    @Log(title = "审批请假请求", businessType = BusinessType.UPDATE)
    @PutMapping("/approveLeave")
    public AjaxResult approveLeave(@RequestBody WsdLeaveRecords WsdLeaveRecords) {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        Long userId = user.getUserId();
        WsdLeaveRecords.setSchoolApproverId(userId);
        WsdLeaveRecords.setSchoolId(schoolId);
        return toAjax(wsdLeaveRecordsService.appoveLeave(WsdLeaveRecords));
    }

    /**
     * 审批提现
     */
    @PreAuthorize("@ss.hasPermi('business:leaves:payouts')")
    @PutMapping("/payouts")
    @Anonymous
    public AjaxResult payouts(@RequestBody PayoutsDTO payoutsDTO) {

        return toAjax(wsdLeaveRecordsService.updateWsdMealRefunds(payoutsDTO));
    }
}
